package wadassignment1

import static org.junit.Assert.*
import org.junit.*
import org.junit.rules.ExpectedException;

class BidIntegrationTests {

	@Before
	void setUp() {
		// Setup logic here
	}

	@After
	void tearDown() {
		// Tear down logic here
	}


	//Not a needed test, but still good to ensure
	void testFirstBidGreaterThanStartingPrice(){
		Listing listing = new Listing();
		listing.setName("2010 Ford Focus");
		listing.setEndDateTime(new Date()+10);
		listing.setStartingBid(10.00);

		Customer seller = new Customer(emailAddress:"bill@espn.com",password:"thispass")
		seller.save(flush:true, failOnError:true)
		listing.seller = seller;

		assertTrue(listing.validate());

		listing.save(flush:true);

		Customer bidder = new Customer(emailAddress:"steve@espn.com",password:"thispass")
		bidder.save(flush:true, failOnError:true)

		Bid b = new Bid(amount:8.00, bidder:bidder)
		listing.addToBids(b);

		assertFalse(b.validate(['amount']))

		Bid b2 = new Bid(amount:15.00, bidder:bidder)
		listing.addToBids(b2);

		assertTrue(b2.validate(['amount']))

	}

	//B-5: The Bid amount must be at least .50 higher than the previous Bid for the same listing (integration test)
	void testBidAmount50CentsHigher(){
		Listing listing = new Listing();
		listing.setName("2010 Ford Focus");
		listing.setEndDateTime(new Date()+10);
		listing.setStartingBid(10.00);

		Customer seller = new Customer(emailAddress:"bill@espn.com",password:"thispass")
		seller.save(flush:true, failOnError:true)
		listing.seller = seller;

		assertTrue(listing.validate());
		assertNotNull(listing.save(flush:true))

		Customer bidder = new Customer(emailAddress:"steve@espn.com",password:"thispass")
		bidder.save(flush:true, failOnError:true)

		Bid b = new Bid(amount:10.00, bidder:bidder)
		listing.addToBids(b);

		assertTrue(b.validate(['amount']))
		assertNotNull(listing.save(flush:true))

		b = new Bid(amount:10.25, bidder:bidder)
		listing.addToBids(b);

		assertFalse(b.validate(['amount']))
		assertNull(listing.save(flush:true))

		if(!b.validate(['amount']))
			listing.bids.remove(b);


		b = new Bid(amount:10.35, bidder:bidder)
		listing.addToBids(b);

		assertFalse(b.validate(['amount']))
		assertNull(listing.save(flush:true))

		if(!b.validate(['amount']))
			listing.bids.remove(b);

		b = new Bid(amount:10.50, bidder:bidder)
		listing.addToBids(b);

		assertTrue(b.validate(['amount']))
		assertNotNull(listing.save(flush:true))

		b = new Bid(amount:20.00, bidder:bidder)
		listing.addToBids(b);

		assertTrue(b.validate(['amount']))
		assertNotNull(listing.save(flush:true, failOnError:true))

		b = new Bid(amount:8.00, bidder:bidder)
		listing.addToBids(b);

		assertFalse(b.validate(['amount']))
		assertNull(listing.save(flush:true))

		if(!b.validate(['amount']))
			listing.bids.remove(b);

	}

	//B-6: When a Listing is saved, any new Bids added for the listing must be saved (integration test)
	void testBidsAddedToListingSaved(){
		Listing listing = new Listing();
		listing.setName("2010 Ford Focus");
		listing.setEndDateTime(new Date()+10);
		listing.setStartingBid(10.00);

		Customer seller = new Customer(emailAddress:"bill@espn.com",password:"thispass")
		seller.save(flush:true, failOnError:true)
		listing.seller = seller;

		Customer bidder = new Customer(emailAddress:"steve@espn.com",password:"thispass")
		bidder.save(flush:true, failOnError:true)

		Bid b = new Bid(amount:15.00, bidder:bidder)
		listing.addToBids(b);

		assertTrue(listing.validate());
		assertNotNull(listing.save(flush:true, failOnError:true))

		Listing returnedListing = Listing.findByName("2010 Ford Focus");
		assertEquals(1,returnedListing.bids.size())

		b = new Bid(amount:18.00, bidder:bidder)
		returnedListing.addToBids(b);

		assertNotNull(returnedListing.save(flush:true, failOnError:true))

		returnedListing = Listing.findByName("2010 Ford Focus");
		assertEquals(2,returnedListing.bids.size())
	}
}
